#region

#endregion

namespace ChumChase.Presenters
{
    using Caliburn.MVP.Presenters;
    using Model;
    using Services;

    public interface ISettingsPresenter : IPresenter
    {
    }

    public class SettingsPresenter : Presenter, ISettingsPresenter
    {
        private readonly IUserSettings _settings;
        private readonly ILocalStorage _storage;
        private IFeedProvider _currentProvider;

        public SettingsPresenter(IFeedProviderCatalog catalog,
                                 ILocalStorage storage,
                                 IUserSettings settings)
        {
            _storage = storage;
            _settings = settings;
            Catalog = catalog;
            CurrentProvider = Catalog.Providers[0];
        }

        public IFeedProviderCatalog Catalog { get; set; }

        public IFeedProvider CurrentProvider
        {
            get { return _currentProvider; }
            set
            {
                _currentProvider = value;
                NotifyOfPropertyChange("CurrentProvider");
            }
        }

        public void ApplySettings()
        {
            foreach (IFeedProvider provider in Catalog.Providers)
            {
                if (provider.Settings.IsValid())
                {
                    provider.ApplySettings();
                    provider.SaveSettings(_storage);

                    var definition = new FeedDefinition();
                    definition.Provider = provider.GetType().Name;
                    _settings.FeedDefinitions.Add(definition);
                }
            }

            _storage.Save(_settings, UserSettings.FileName);
        }
    }
}